<?php
$tradesForRound = getTradesForRound($loggedInTeam->id);
$myTeam = new SelectedTeam(getSelectedTeamForCurrentRound($loggedInTeam->id, true), $loggedInTeam->teamName);
$players = $myTeam->getPlayers();
$currentRound = getCurrentRound();
?>
<script>
function handleReasonChange(frm) {
	if (frm.reasonType.options[frm.reasonType.selectedIndex].value == "SPUD") {
		playerId = document.getElementById('playerOneSelect').options[document.getElementById('playerOneSelect').selectedIndex].value;
		for (i=0; i < spudPlayers.length; i++) {
			if (spudPlayers[i].playerId == playerId) {
				if (spudPlayers[i].roundsMissed < 6) {
					alert(spudPlayers[i].playerDescription + " has only missed " + spudPlayers[i].roundsMissed + " games so can not be spudded out.");
					frm.reasonType.selectedIndex = -1;
				}
				
				return;
			}
		}	
	}
	if (frm.reasonType.options[frm.reasonType.selectedIndex].value == "<?echo TradeType::Lti?>" || frm.reasonType.options[frm.reasonType.selectedIndex].value == "<?echo TradeType::Injury?>") {
		document.getElementById("sourceDiv").style.display = "block";
	}
	else {
		document.getElementById("sourceDiv").style.display = "none";
	}
}
function submitLtiSpud(frm) {
   if (frm.playerOne.selectedIndex == 0) {
      alert("Need to select a player to replace.");
      return;
   }
   if (frm.availPlayers.selectedIndex == -1) {
      alert("Need to select a replacement player.");
      return;
   }
   if (frm.reasonType.selectedIndex == 0) {
      alert("Need to select a reason.");
      return;
   }
   if (frm.reasonType.options[frm.reasonType.selectedIndex].value == "<?echo TradeType::Lti?>" || frm.reasonType.options[frm.reasonType.selectedIndex].value == "<?echo TradeType::Injury?>") {
      if (frm.source.value == "") {
         alert("Need to provide a source.");
         return;
      }
   }

   frm.submit();
}
function SpudPlayer(playerId, playerDescription, roundsMissed) {
	this.playerId = playerId;
	this.playerDescription = playerDescription;
	this.roundsMissed = roundsMissed;
}
var spudPlayers = new Array();
<?
$count = count($players);
for ($i = 0; $i < $count; $i++) {?>
	spudPlayers[<?echo $i?>] = new SpudPlayer(<?echo $players[$i]->player->id?>, "<?echo $players[$i]->player->getDescription()?>", <?echo $players[$i]->roundsSincePlayed?>);
<?}?>
</script>
<form id="ltiForm" method="post" action="submitLtiSpud.php">
<b>A maximum of 2 trades can be made per round</b>
<?if ($currentRoundStatus != RoundStatusType::Open) {?><font color='red'>LTI's and other trades can only be made outside of Lockout's</font><?}?>
<?if ($tradesForRound == 2) {?><font color='red'>You have reached your limit of 2 trades for this round</font><?}?>
<table>
	<tr>
		<td valign="top">
		<select name="playerOne" id="playerOneSelect" <?if ($currentRoundStatus != RoundStatusType::Open || $tradesForRound == 2) {?>disabled<?}?>>
                   <option value="">Select Player</option>
		<?
		$count = count($players);
		for ($i = 0; $i < $count; $i++) {?>
			<option value="<?echo $players[$i]->player->id?>"><?echo $players[$i]->player->getDescription()?></option>
		<?}?>
		</select>
		</td>
		<td valign="top">
			<table>
				<tr>
					<td></td>
					<td><input type="text" onkeyup="filterSelect(this.value, this.form, false)" <?if ($currentRoundStatus != RoundStatusType::Open || $tradesForRound == 2) {?>disabled<?}?>></input>&nbsp;Start typing name to filter list</td>
				</tr>
				<tr>
					<td colspan="2">
						<table>
							<tr>
								<td>
									<select name="availPlayers" size="20" <?if ($currentRoundStatus != RoundStatusType::Open || $tradesForRound == 2) {?>disabled<?}?>>
									</select> 
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
		<td valign="top">
			<select name="reasonType" onchange="handleReasonChange(this.form)" <?if ($currentRoundStatus != RoundStatusType::Open || $tradesForRound == 2) {?>disabled<?}?>>
				<option value="">Select Reason</option>
				<option value="LTI">LTI(6+ weeks)</option>
				<option value="SPUD">Spud(6 weeks)</option>
				<option value="INJURY">Injury(4+ weeks)</option>
				<option value="RETIRED">Retired</option>
				<option value="SUSPENDED">Suspended(4+ weeks)</option>
			</select>
			<div id="sourceDiv" style="display: none">
				source:<input type="text" name="source">
			</div> 
		</td>
	</tr>
	<tr>
		<td><input type="button" value="Update" onclick="submitLtiSpud(this.form)" class="buttonStyle" <?if ($currentRoundStatus != RoundStatusType::Open  || $tradesForRound == 2) {?>disabled<?}?>></td>
	</tr>			
</table>
</form>

<script>populateSelect(document.getElementById("ltiForm"), false);</script>